/**
 * Generate code from /{{projectName}}-core/src/main/java/{{packageName}}/{{apis}}/rest/Abstract{{apiEntities}}Resource.java.hbs
 */
package cn.ibizlab.api.rest;

import java.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ObjectUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.ApiOperation;
import cn.ibizlab.util.domain.ImportResult;
import cn.ibizlab.api.dto.*;
import cn.ibizlab.api.mapping.*;
import cn.ibizlab.core.system.domain.SysPermission;
import cn.ibizlab.core.system.service.SysPermissionService;
import cn.ibizlab.core.system.filter.SysPermissionSearchContext;

/**
 * 实体[SysPermission] rest实现
 *
 * @author generator
 */
@Slf4j
public abstract class AbstractSysPermissionResource {

    @Autowired
    public SysPermissionService sysPermissionService;

    @Autowired
    @Lazy
    public SysPermissionDTOMapping sysPermissionDtoMapping;

    @Autowired
    @Lazy
    public SysPermissionFilterDTOMapping sysPermissionFilterDtoMapping;


    /**
    * 创建Create 权限资源
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysPermissionDTO>
    */
    @ApiOperation(value = "创建Create", tags = {"权限资源" },  notes = "SysPermission-Create ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysPermission-Create-all')")
    @PostMapping(value = {"sys_permissions","sys_permissions"})
    public ResponseEntity<SysPermissionDTO> create
            (@Validated @RequestBody SysPermissionDTO dto) {
        SysPermission domain = sysPermissionDtoMapping.toDomain(dto);
        sysPermissionService.create(domain);
        SysPermission rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysPermissionDtoMapping.toDto(rt));
    }

    /**
    * 获取Get 权限资源
    * 
    *
    * @param id id
    * @return ResponseEntity<SysPermissionDTO>
    */
    @ApiOperation(value = "获取Get", tags = {"权限资源" },  notes = "SysPermission-Get ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysPermission-Get-all')")
    @GetMapping("sys_permissions/{id}")
    public ResponseEntity<SysPermissionDTO> getById
            (@PathVariable("id") String id) {
        SysPermission rt = sysPermissionService.get(id);
        return ResponseEntity.status(HttpStatus.OK).body(sysPermissionDtoMapping.toDto(rt));
    }

    /**
    * 删除Remove 权限资源
    * 
    *
    * @param id id
    * @return ResponseEntity<Boolean>
    */
    @ApiOperation(value = "删除Remove", tags = {"权限资源" },  notes = "SysPermission-Remove ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysPermission-Remove-all')")
    @DeleteMapping("sys_permissions/{id}")
    public ResponseEntity<Boolean> removeById
            (@PathVariable("id") String id) {
        Boolean rt = sysPermissionService.remove(id);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 更新Update 权限资源
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<SysPermissionDTO>
    */
    @ApiOperation(value = "更新Update", tags = {"权限资源" },  notes = "SysPermission-Update ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysPermission-Update-all')")
    @PutMapping("sys_permissions/{id}")
    public ResponseEntity<SysPermissionDTO> updateById
            (@PathVariable("id") String id, @Validated @RequestBody SysPermissionDTO dto) {
        SysPermission domain = sysPermissionDtoMapping.toDomain(dto);
        domain.setId(id);
        sysPermissionService.update(domain);
        SysPermission rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysPermissionDtoMapping.toDto(rt));
    }

    /**
    * 校验CheckKey 权限资源
    * 
    *
    * @param dto dto
    * @return ResponseEntity<Integer>
    */
    @ApiOperation(value = "校验CheckKey", tags = {"权限资源" },  notes = "SysPermission-CheckKey ")
    @PostMapping("sys_permissions/checkkey")
    public ResponseEntity<Integer> checkKey
            (@Validated @RequestBody SysPermissionDTO dto) {
        SysPermission domain = sysPermissionDtoMapping.toDomain(dto);
        Integer rt = sysPermissionService.checkKey(domain);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 草稿GetDraft 权限资源
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysPermissionDTO>
    */
    @ApiOperation(value = "草稿GetDraft", tags = {"权限资源" },  notes = "SysPermission-GetDraft ")
    @GetMapping("sys_permissions/getdraft")
    public ResponseEntity<SysPermissionDTO> getDraft
            (@SpringQueryMap SysPermissionDTO dto) {
        SysPermission domain = sysPermissionDtoMapping.toDomain(dto);
        SysPermission rt = sysPermissionService.getDraft(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysPermissionDtoMapping.toDto(rt));
    }

    /**
    * 保存Save 权限资源
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysPermissionDTO>
    */
    @ApiOperation(value = "保存Save", tags = {"权限资源" },  notes = "SysPermission-Save ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysPermission-Save-all')")
    @PostMapping("sys_permissions/save")
    public ResponseEntity<SysPermissionDTO> save
            (@Validated @RequestBody SysPermissionDTO dto) {
        SysPermission domain = sysPermissionDtoMapping.toDomain(dto);
        sysPermissionService.save(domain);
        SysPermission rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysPermissionDtoMapping.toDto(rt));
    }

    /**
    * 查询FetchDefault 权限资源
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysPermissionDTO>>
    */
    @ApiOperation(value = "查询FetchDefault", tags = {"权限资源" },  notes = "SysPermission-FetchDefault ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysPermission-FetchDefault-all')")
    @PostMapping("sys_permissions/fetchdefault")
    public ResponseEntity<List<SysPermissionDTO>> fetchDefault
            (@Validated @RequestBody SysPermissionFilterDTO dto) {
        SysPermissionSearchContext context = sysPermissionFilterDtoMapping.toDomain(dto);
        Page<SysPermission> domains = sysPermissionService.searchDefault(context) ;
        List<SysPermissionDTO> list = sysPermissionDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }


    /**
    * 批量新建权限资源
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysPermission-Create-all')")
    @ApiOperation(value = "批量新建权限资源", tags = {"权限资源" },  notes = "批量新建权限资源")
	@PostMapping("sys_permissions/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<SysPermissionDTO> dtos) {
        sysPermissionService.createBatch(sysPermissionDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量删除权限资源
    * @param ids ids
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysPermission-Remove-all')")
    @ApiOperation(value = "批量删除权限资源", tags = {"权限资源" },  notes = "批量删除权限资源")
	@DeleteMapping("sys_permissions/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        sysPermissionService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量更新权限资源
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysPermission-Update-all')")
    @ApiOperation(value = "批量更新权限资源", tags = {"权限资源" },  notes = "批量更新权限资源")
	@PutMapping("sys_permissions/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<SysPermissionDTO> dtos) {
        sysPermissionService.updateBatch(sysPermissionDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量保存权限资源
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysPermission-Save-all')")
    @ApiOperation(value = "批量保存权限资源", tags = {"权限资源" },  notes = "批量保存权限资源")
	@PostMapping("sys_permissions/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<SysPermissionDTO> dtos) {
        sysPermissionService.saveBatch(sysPermissionDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量导入权限资源
    * @param config 导入模式
    * @param ignoreError 导入中忽略错误
    * @return ResponseEntity<ImportResult>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysPermission-Save-all')")
    @ApiOperation(value = "批量导入权限资源", tags = {"权限资源" },  notes = "批量导入权限资源")
	@PostMapping("sys_permissions/import")
    public ResponseEntity<ImportResult> importData(@RequestParam(value = "config" , required = false) String config ,@RequestParam(value = "ignoreerror", required = false, defaultValue = "true") Boolean ignoreError ,@RequestBody List<SysPermissionDTO> dtos) {
        return  ResponseEntity.status(HttpStatus.OK).body(sysPermissionService.importData(config,ignoreError,sysPermissionDtoMapping.toDomain(dtos)));
    }

}
